package com.xuecheng.learning.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.content.model.dto.XcChooseCourseDto;
import com.example.content.model.dto.XcCourseTablesDto;
import com.example.content.model.pojo.CoursePublish;
import com.example.content.model.pojo.XcChooseCourse;
import com.example.content.model.pojo.XcCourseTables;
import com.xuecheng.config.XueChengPlusException;
import com.xuecheng.feginclient.ContentServiceClient;
import com.xuecheng.learning.mapper.XcChooseCourseMapper;
import com.xuecheng.learning.mapper.XcCourcseTablesMapper;
import com.xuecheng.learning.service.MyCourseTablesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class MyCourseTablesServiceImpl implements MyCourseTablesService {
    @Resource
    ContentServiceClient contentServiceClient;
    @Resource
    XcChooseCourseMapper xcChooseCourseMapper;
    @Resource
    XcCourcseTablesMapper xcCourcseTablesMapper;
    @Override
    public XcChooseCourseDto addChooseCourse(String userId, Long courseId) {
//        查询课程信息
        CoursePublish coursepublish = contentServiceClient.getCoursepublish(courseId);
//        课程收费标准
        String charge = coursepublish.getCharge();
//        选课记录
        XcChooseCourse xcChooseCourse = null;
        if ("201000".equals(charge)){
//            添加免费课程
            xcChooseCourse=addFreeCourse(userId,coursepublish);
//            添加到我的课程表
            XcCourseTables xcCourseTables = addCourseTabls(xcChooseCourse);
        }else {
//            添加收费课程
            xcChooseCourse=addChargeCourse(userId,coursepublish);
        }
        //        获取学习资格
        XcCourseTablesDto xcCourseTablesDto = getLearningStatus(userId, courseId);

        //构造返回值
        XcChooseCourseDto xcChooseCourseDto = new XcChooseCourseDto();
        BeanUtils.copyProperties(xcChooseCourse,xcChooseCourseDto);
        //设置学习资格状态
        xcChooseCourseDto.setLearnStatus(xcCourseTablesDto.getLearnStatus());

        return xcChooseCourseDto;
    }
/*
* 判断学习资格
* */
    @Override
    public XcCourseTablesDto getLearningStatus(String userId, Long courseId) {
//        查询我的课程表
        XcCourseTables xcCourseTables = getXcCourseTables(userId, courseId);
        if (xcCourseTables==null){
            XcCourseTablesDto xcCourseTablesDto = new XcCourseTablesDto();
            //没有选课或选课后没有支付
            xcCourseTablesDto.setLearnStatus("702002");
            return xcCourseTablesDto;
        }
        XcCourseTablesDto xcCourseTablesDto = new XcCourseTablesDto();
        BeanUtils.copyProperties(xcCourseTables,xcCourseTablesDto);
        //是否过期,true过期，false未过期
        boolean b = xcCourseTables.getValidtimeEnd().isBefore(LocalDateTime.now());
        if (!b){
//            没有过期
            BeanUtils.copyProperties(xcCourseTables,xcCourseTablesDto);
            xcCourseTablesDto.setLearnStatus("702001");
            return xcCourseTablesDto;
        }else {
//            已经过期
            BeanUtils.copyProperties(xcCourseTables,xcCourseTablesDto);
            xcCourseTablesDto.setLearnStatus("702003");
            return xcCourseTablesDto;
        }
    }

    //添加收费课程
    private XcChooseCourse addChargeCourse(String userId, CoursePublish coursepublish) {
        LambdaQueryWrapper<XcChooseCourse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(XcChooseCourse::getUserId,userId)
                .eq(XcChooseCourse::getCourseId,coursepublish.getId())
                .eq(XcChooseCourse::getOrderType,"700002") //收费订单
                .eq(XcChooseCourse::getStatus,"701002");//待支付
        List<XcChooseCourse> xcChooseCourses = xcChooseCourseMapper.selectList(queryWrapper);
        if (xcChooseCourses!=null&&xcChooseCourses.size()>0){
           return xcChooseCourses.get(0);
        }
        XcChooseCourse xcChooseCourse = new XcChooseCourse();
        xcChooseCourse.setCourseId(coursepublish.getId());
        xcChooseCourse.setCourseName(coursepublish.getName());
        xcChooseCourse.setUserId(userId);
        xcChooseCourse.setCompanyId(coursepublish.getCompanyId());
        xcChooseCourse.setOrderType("700002");
        xcChooseCourse.setCreateDate(LocalDateTime.now());
        xcChooseCourse.setStatus("701002");
        xcChooseCourse.setValidDays(coursepublish.getValidDays());
        xcChooseCourse.setValidtimeStart(LocalDateTime.now());
        xcChooseCourse.setValidtimeEnd(LocalDateTime.now().plusDays(365));
        xcChooseCourseMapper.insert(xcChooseCourse);
        return xcChooseCourse;
    }

    //添加到我的课程表
    private XcCourseTables addCourseTabls(XcChooseCourse xcChooseCourse) {
        //选课记录完成且未过期可以添加课程到课程表
        String status = xcChooseCourse.getStatus();
        if (!"701001".equals(status)){
            XueChengPlusException.cast("选课未完成");
        }
//        查询我得课程表
        XcCourseTables xcCourseTables = getXcCourseTables(xcChooseCourse.getUserId(),xcChooseCourse.getCourseId());
        if (xcCourseTables!=null){
            return xcCourseTables;
        }
        XcCourseTables xcCourseTables1 = new XcCourseTables();
        xcCourseTables1.setChooseCourseId(xcChooseCourse.getId());
        xcCourseTables1.setCourseName(xcChooseCourse.getCourseName());
        xcCourseTables1.setUserId(xcChooseCourse.getUserId());
        xcCourseTables1.setCompanyId(xcChooseCourse.getCompanyId());
        xcCourseTables1.setCourseId(xcChooseCourse.getCourseId());
        xcCourseTables1.setCreateDate(LocalDateTime.now());
        xcCourseTables1.setValidtimeStart(xcChooseCourse.getValidtimeStart());
        xcCourseTables1.setValidtimeEnd(xcChooseCourse.getValidtimeEnd());
        xcCourseTables1.setCourseType(xcChooseCourse.getOrderType());
        xcCourcseTablesMapper.insert(xcCourseTables1);
        return xcCourseTables1;
    }
    /**
     * @description 根据课程和用户查询我的课程表中某一门课程
     * @param userId
     * @param courseId
     * @return com.xuecheng.learning.model.po.XcCourseTables
     * @author Mr.M
     * @date 2022/10/2 17:07
     */
    private XcCourseTables getXcCourseTables(String userId, Long courseId) {
        LambdaQueryWrapper<XcCourseTables> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(XcCourseTables::getUserId,userId).eq(XcCourseTables::getCourseId,courseId);
        XcCourseTables xcCourseTables = xcCourcseTablesMapper.selectOne(queryWrapper);
        return xcCourseTables;
    }

    //添加免费课程,免费课程加入选课记录表、我的课程表
    private XcChooseCourse addFreeCourse(String userId, CoursePublish coursepublish) {
        //查询选课记录表是否存在免费的且选课成功的订单
        LambdaQueryWrapper<XcChooseCourse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper=queryWrapper.eq(XcChooseCourse::getUserId,userId)
                .eq(XcChooseCourse::getCourseId,coursepublish.getId())
                .eq(XcChooseCourse::getOrderType,"700001")//免费课程
                .eq(XcChooseCourse::getStatus,"701001");//选课成功;
        List<XcChooseCourse> xcChooseCourses = xcChooseCourseMapper.selectList(queryWrapper);
        if (xcChooseCourses!=null&&xcChooseCourses.size()>0){
            return xcChooseCourses.get(0);
        }
        XcChooseCourse xcChooseCourse = new XcChooseCourse();
        xcChooseCourse.setCourseId(coursepublish.getId());
        xcChooseCourse.setCourseName(coursepublish.getName());
        xcChooseCourse.setCoursePrice(0f);
        xcChooseCourse.setUserId(userId);
        xcChooseCourse.setCompanyId(coursepublish.getCompanyId());
        xcChooseCourse.setOrderType("700001");
        xcChooseCourse.setCreateDate(LocalDateTime.now());
        xcChooseCourse.setStatus("701001");
        xcChooseCourse.setValidDays(365);
        xcChooseCourse.setValidtimeStart(LocalDateTime.now());
        xcChooseCourse.setValidtimeEnd(LocalDateTime.now().plusDays(365));
        xcChooseCourseMapper.insert(xcChooseCourse);
        return xcChooseCourse;
    }
}
